﻿#include "ZPageWidget.h"
#include <QRegExpValidator> 

ZPageWidget::ZPageWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	ui.lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));

	connect(ui.btn_previous, SIGNAL(clicked()), this, SLOT(slot_previousPageBtnClicked()));
	connect(ui.btn_next, SIGNAL(clicked()), this, SLOT(slot_nextPageBtnClicked()));
	connect(ui.btn_first, SIGNAL(clicked()), this, SLOT(slot_firstPageBtnClicked()));
	connect(ui.btn_last, SIGNAL(clicked()), this, SLOT(slot_lastPageBtnClicked()));
    connect(ui.btn_skip, SIGNAL(clicked()), this, SLOT(slot_skipPageBtnClicked()));
    ui.lineEdit->setVisible(false);
    ui.btn_skip->setVisible(false);
}

ZPageWidget::~ZPageWidget()
{
}

void ZPageWidget::setPageCount(int count)
{
	m_currentPage = 1;
	m_pageCount = count;
	m_pageCount = m_pageCount > 0 ? m_pageCount : 1;
	if (m_currentPage > m_pageCount)
	{
		m_currentPage = m_pageCount;
	}

	pageChanged();
}

void ZPageWidget::setCurrentPage(int page)
{
	m_currentPage = page;
}

void ZPageWidget::setDataCount(int cnt)
{
	m_dataCount = cnt;
    ui.lb_totalData->setText(QString("%1:%2").arg(QStringLiteral("总数")).arg(cnt));
}

void ZPageWidget::setPerpageDataCount(int cnt)
{
	m_perpageDataCount = cnt;
    ui.lb_perPageData->setText(QString("%1:%2").arg(QStringLiteral("每页")).arg(cnt));
}

void ZPageWidget::pageChanged()
{
	emit sign_pageChanged(m_currentPage);
	ui.lb_pageInfo->setText(QString("%1/%2").arg(m_currentPage).arg(m_pageCount));
}

void ZPageWidget::slot_previousPageBtnClicked()
{
	if (m_currentPage > 1)
	{
		m_currentPage--;
		pageChanged();
	}
}

void ZPageWidget::slot_nextPageBtnClicked()
{
	if (m_currentPage < m_pageCount)
	{
		m_currentPage++;
		pageChanged();
	}
}

void ZPageWidget::slot_firstPageBtnClicked()
{
	if (m_currentPage != 1)
	{
		m_currentPage = 1;
		pageChanged();
	}
}

void ZPageWidget::slot_lastPageBtnClicked()
{
	if (m_pageCount != m_currentPage)
	{
		m_currentPage = m_pageCount;
		pageChanged();
	}
}

void ZPageWidget::slot_skipPageBtnClicked()
{
	int page = ui.lineEdit->text().toInt();
	ui.lineEdit->clear();
	if (page > 0 && page <= m_pageCount)
	{
		m_currentPage = page;
		pageChanged();
	}
}
